<template>
	<view>
		<view class="top-tabbar">
			<view :class="state_active == 0 ? 'tab-item active' : 'tab-item'" @click="stateFunc(0)">未使用</view>
			<view :class="state_active == 1 ? 'tab-item active' : 'tab-item'" @click="stateFunc(1)">已使用</view>
			<view :class="state_active == 2 ? 'tab-item active' : 'tab-item'" @click="stateFunc(2)">已过期</view>
		</view>
		<!--列表-->
		<scroll-view scroll-y="true" class="scroll-Y" :style="'height:' + scrollviewHigh + 'px;'" lower-threshold="50"
			@scrolltoupper="scrolltoupperFunc" @scrolltolower="scrolltolowerFunc">
			<view class="p30 bg-white">
				<view class="item-wrap mb20" v-for="(item, index) in DataList" :key="index">
					<view
						:class="item.is_expire==0&&item.is_use==0 ? 'coupon-item coupon-item-'+item.color.text : 'coupon-item coupon-item-gray'"
						@click="lookRule(item)">
						<!--装饰用的小圆-->
						<view class="circles">
							<text v-for="(circle, num) in 8" :key="num"></text>
						</view>
						<view class="info">
							<view>{{item.coupon_type.text}}</view>
						</view>
						<view class="operation d-b-c">
							<view class="flex-1 coupon-content">
								<view>
									<template v-if=" item.coupon_type.value == 10 ">
										<view class="price">
											<text>￥</text>
											<text class="f40 fb">{{ item.reduce_price }}</text>
										</view>
									</template>
									<template v-if="item.coupon_type.value == 20 ">
										<text class="f40 fb">{{ item.discount }}</text><text>折</text>
									</template>
								</view>
								<view class="f30">{{item.name}}</view>
								<view class="f24">
									<template v-if="item.expire_type ==10">
										有效期：剩余{{item.expire_day}}天
									</template>
									<template v-if="item.expire_type ==20">
										有效期：{{item.start_time.text}}至{{item.end_time.text}}
									</template>
								</view>
							</view>
							<view class="right-box d-c-c">
								<view type="default" v-if="item.state.value>0" class="f30">
									未使用
								</view>
								<view v-else class="f30">
									<text>{{item.state.text}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="">
					<view class="bottom-refresh">
						<view class="d-c-c p30" v-if="loading">
							<text class="gray3">加载中...</text>
						</view>
						<view class="d-c-c p30" v-if="no_more ">
							<text class="gray3">~~加载完成~~</text>
						</view>
					</view>
				</view>
				<view class="d-c-c p30" v-if="DataList.length==0 && !loading">
					<text class="iconfont icon-wushuju"></text>
					<text class="cont">亲，暂无相关记录哦</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				/*手机高度*/
				phoneHeight: 0,
				/*可滚动视图区域高度*/
				scrollviewHigh: 0,
				/*状态选中*/
				state_active: 0,
				/*列表*/
				DataList: {},
				no_more: false,
				loading: false,
				data_type: 'not_use'
			};
		},
		mounted() {
			/*初始化*/
			this.init();
			/*获取数据*/
			this.getData();
		},
		methods: {
			/*初始化*/
			init() {
				let self = this;
				uni.getSystemInfo({
					success(res) {
						self.phoneHeight = res.windowHeight;
						// 计算组件的高度
						let view = uni.createSelectorQuery().select('.top-tabbar');
						view.boundingClientRect(data => {
							let h = self.phoneHeight - data.height;
							self.scrollviewHigh = h;
						}).exec();
					}
				});
			},

			/*获取数据*/
			getData() {

				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				let data_type = self.data_type;
				self._get('user.coupon/lists', {
					data_type: data_type,
				}, function(res) {
					uni.hideLoading();
					self.DataList = res.data.list;
				});
			},

			/*状态切换*/
			stateFunc(e) {
				let self = this;
				if (self.state_active != e) {
					if (e == 0) {
						self.data_type = 'not_use';
					}
					if (e == 1) {
						self.data_type = 'is_use';
					}
					if (e == 2) {
						self.data_type = 'is_expire';
					}
					self.state_active = e;
					self.getData();
				}
			},

			/*查看规则*/
			lookRule(item) {
				item.rule = true;
			},

			/*关闭规则*/
			closeRule(item) {
				item.rule = false;
			}
		}
	};
</script>

<style></style>
